/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.JDBC.core;

import core.*;
import core.comparators.ComparatorFactory;
import core.comparators.CourseComparator.COURSECOMPARATORTYPE;
import db.JDBC.*;
import db.core.interfaces.CourseDAOCore;
import db.interfaces.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
/**
 *
 * @author Anton
 */
public class CourseJdbcDAOCore implements CourseDAOCore
{
    private static CourseJdbcDAOCore instance = null;
    private CourseDAO courseDAO;
    private CRUDJdbcDAOCore crud;

    /**
     * 
     */
    private CourseJdbcDAOCore()
    {
        JdbcDaoFactory daoFactory = JdbcDaoFactory.getInstance();
        courseDAO = daoFactory.getCourseJdbcDao();
        crud = CRUDJdbcDAOCore.getInstance();
    }

    /**
     * 
     * @return
     */
    public static CourseJdbcDAOCore getInstance()
    {
        if(instance == null)
            instance = new CourseJdbcDAOCore();
        return instance;
    }

    /**
     * 
     * @param id
     * @return
     */
    public Course getCourseById(long id)
    {
        return crud.getCourse(id);
    }

    /**
     *
     * @param beginsWith
     * @param maxResult
     * @return
     */
    public Collection<String> searchForStringCourses(String beginsWith, int maxResult)
    {
        Collection<Course> names  = courseDAO.searchForCoursesByName(beginsWith);
        Collection<Course> courseIds = courseDAO.searchForCoursesByCourseId(beginsWith);

        Collection<String> t = new TreeSet<String>();
        List<String> returnCollection = new ArrayList<String>();
        if(names != null)
            for(Course c : names)
                if(c != null)
                    t.add(c.getName());
        if(courseIds != null)
            for(Course c : courseIds)
                if(c != null)
                    t.add(c.getCourseId());

        for(String s : t)
            if(s != null)
                returnCollection.add(s);

        if(returnCollection.size() < maxResult)
            maxResult = returnCollection.size();
        return returnCollection.subList(0, maxResult);
    }
    /**
     *
     * @param beginsWith
     * @return
     */
    public Collection<Course> searchForObjectCourses(String beginsWith)
    {
        Collection<Course> names  = courseDAO.searchForCoursesByName(beginsWith);
        Collection<Course> courseIds = courseDAO.searchForCoursesByCourseId(beginsWith);

        Collection<Course> t = new TreeSet<Course>(ComparatorFactory.getCourseComparator(COURSECOMPARATORTYPE.ID, true));
        List<Course> returnCollection = new ArrayList<Course>();
        
        if(names != null)
            for(Course c : names)
                if(c != null)
                    t.add(c);
        if(courseIds != null)
            for(Course c : courseIds)
                if(c != null)
                    t.add(c);
        for(Course c : t)
            if(c != null)
                returnCollection.add(c);
        Collections.sort(returnCollection, ComparatorFactory.getCourseComparator(COURSECOMPARATORTYPE.NAME, true));

        return returnCollection;
    }

    /**
     * 
     * @param name
     * @return
     */
    public Collection<Course> getCoursesByName(String name)
    {
        if(name == null)
            return null;
        Collection<Course> courses = new ArrayList<Course>();
        for(Course c : courseDAO.getCoursesByName(name))
            if( c!= null)
                courses.add(crud.getCourse(c.getId()));
        return courses;
    }
    /**
     * 
     * @param courseId
     * @return
     */
    public Collection<Course> getCoursesByCourseId(String courseId)
    {
        if(courseId == null)
            return null;
        Collection<Course> courses = new ArrayList<Course>();
        for(Course c : courseDAO.getCoursesByCourseId(courseId))
            if(c != null)
                courses.add(crud.getCourse(c.getId()));
        return courses;
    }
    /**
     * 
     * @return
     */
    public Collection<Course> getAllCourses()
    {
        Collection<Course> courses = new ArrayList<Course>();
        for(Course c : courseDAO.getAllCourses())
            if(c != null)
                courses.add(crud.getCourse(c.getId()));
        return courses;
    }

    /**
     *
     * @param course
     * @param book
     */
    public void addBookToCourse(Course course, Book book)
    {
        if(course == null || book == null)
            return;
        crud.addBookToCourse(course, book);
    }
    /**
     * 
     * @param course
     * @param book
     */
    public void deleteBookFromCourse(Course course, Book book)
    {
        if(course == null || book == null)
            return;
        crud.deleteBookFromCourse(course, book);
    }

    /**
     * 
     * @param course
     */
    public void addCourse(Course course)
    {
        if(course == null)
            return;
        crud.addCourse(course);
    }
    /**
     * 
     * @param course
     */
    public void updateCourse(Course course)
    {
        if(course == null)
            return;
        crud.updateCourse(course);
    }
    /**
     * 
     * @param course
     */
    public void deleteCourse(Course course)
    {
        if(course == null)
            return;
        crud.deleteCourse(course);        
    }
}
